package com.hk.huakangcompany.controller;

import com.github.pagehelper.PageInfo;
import com.hk.huakangcompany.commons.security.CommonResult;
import com.hk.huakangcompany.model.Nav;
import com.hk.huakangcompany.model.vo.Result;
import com.hk.huakangcompany.model.vo.ResultVO;
import com.hk.huakangcompany.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/5 8:54
 * @Statement: 导航
 */
@RestController
@RequestMapping("/nav")
public class NavController {

//    private Jedis jedis = JedisUtil.getJedis();

    @Autowired
    private NavService iNavService;

    @GetMapping("/findAll")
    public ResultVO<Nav> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit
    ) {
        List<Nav> navList = iNavService.findAll(page, limit, new Nav());
        PageInfo<Nav> navPageInfo = new PageInfo<>(navList);
        int count = iNavService.count(-1,-1, new Nav());
        if (navList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,navPageInfo.getList());
            return resultVO;
        } else {
            ResultVO resultVO = CommonResult.successResultVO(count,navPageInfo.getList());
            return resultVO;
        }
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        Nav nav = iNavService.findById(id);
        if (nav.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(nav);
        return result;
//        return null;
    }

    @PostMapping("/save")
    public Result save(@RequestBody(required = false) Nav nav) {
        if (nav == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
//        清楚Redis中的导航栏
//        jedis.del("navList");
        iNavService.save(nav);
//        清楚Redis中的导航栏
//        jedis.del("navList");
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) Nav nav) throws IOException {
        if (nav == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
//        清楚Redis中的导航栏
//        jedis.del("navList");
        iNavService.update(nav);
//        清楚Redis中的导航栏
//        jedis.del("navList");
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/deletetNav/{id}")
    public Result deletetNav(@PathVariable int id) throws IOException {
//        清楚Redis中的导航栏
//        jedis.del("navList");
        iNavService.delete(id);
//        清楚Redis中的导航栏
//        jedis.del("navList");
        Result result = CommonResult.successResult(null);
        return result;
    }






}
